﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok
{
	/// <summary>
	/// This type represents the 'unit type' that expresses an empty type or 'no value' similar to 
	/// <see cref="System.Void"/>, but in contrast to <see cref="System.Void"/> a single instance of None 
	/// can be created. This singleton instance is used to express 'no value' or 'no result'.
	/// </summary>
	[Serializable]
	public sealed class None :
		ICloneable,
		IEquatable<None>
	{
		// Implementation Notes:
		//
		//	Even though this type is designed to be a singleton, meaning to allow only one instance at any time,
		//	it might happen that multiple instances exists. This can be caused by de-serialization, where each 
		//	time a new instance is created. To workaround this issue the type implements custom equality logic.

		// static data
		[NonSerialized]
		private static readonly None sInstance = new None();


		#region ++ Public Static Interface ++

		/// <summary>
		/// Gets a reference to the one-and-only instance.
		/// </summary>
		public static None Instance
		{
			get
			{
				return sInstance;
			}
		}

		#endregion

		#region ++ Public Interface ++

		/// <summary>
		/// Creates a new None instance that is a copy of the current instance.
		/// </summary>
		/// 
		/// <returns>
		/// A new None instance that is a copy of this instance.</returns>
		Object ICloneable.Clone()
		{
			return None.Instance;
		}

		/// <summary>
		/// Creates a new None instance that is a copy of the current instance.
		/// </summary>
		/// 
		/// <returns>
		/// A new None instance that is a copy of this instance.</returns>
		public None Clone()
		{
			return None.Instance;
		}


		/// <summary>
		/// Returns the hash code for the current None instance. 
		/// </summary>
		/// 
		/// <returns>
		/// A 32-bit signed integer hash code.</returns>
		public override Int32 GetHashCode()
		{
			return 0;
		}


		/// <summary>
		/// Determines whether the specified None instance is equal to the current instance.
		/// </summary>
		/// 
		/// <param name="other">
		/// The None instance to compare with the current instance.</param>
		/// 
		/// <returns>
		/// True, if the specified None instance is equal to the current instance; otherwise, False.</returns>
		public override Boolean Equals(Object other)
		{
			return other is None;
		}

		/// <summary>
		/// Determines whether the specified None instance is equal to the current instance.
		/// </summary>
		/// 
		/// <param name="other">
		/// The None instance to compare with the current instance.</param>
		/// 
		/// <returns>
		/// True, if the specified None instance is equal to the current instance; otherwise, False.</returns>
		public Boolean Equals(None other)
		{
			if ((Object)other == null)
				return false;

			return true;
		}


		/// <summary>
		/// Determines whether the specified None instances are equal.
		/// </summary>
		/// 
		/// <param name="a">
		/// The first None instance to compare.</param>
		/// <param name="b">
		/// The second None instance to compare.</param>
		/// <returns>
		/// True, if the specified None instances are equal; otherwise, False.</returns>
		public static Boolean operator ==(None a, None b)
		{
			if ((Object)a == null || (Object)b == null)
				return false;

			return true;
		}

		/// <summary>
		/// Determines whether the specified None instances are unequal.
		/// </summary>
		/// 
		/// <param name="a">
		/// The first None instance to compare.</param>
		/// <param name="b">
		/// The second None instance to compare.</param>
		/// <returns>
		/// True, if the specified None instances are unequal; otherwise, False.</returns>
		public static Boolean operator !=(None a, None b)
		{
			if ((Object)a == null || (Object)b == null)
				return true;

			return false;
		}


		/// <summary>
		/// Returns a string that represents the current None instance.
		/// </summary>
		/// 
		/// <returns>
		/// A string that represents the current None instance.</returns>
		public override String ToString()
		{
			return "(None)";
		}

		#endregion

		#region Implementation

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		private None()
		{
			// required for serialization
		}

		#endregion

	}
}
